package com.shencai.demo.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shencai.demo.base.entity.SysMenu;
import com.shencai.demo.base.entity.SysRole;
import com.shencai.demo.base.service.SysRoleMenuService;
import com.shencai.demo.base.service.SysRoleService;
import com.shencai.demo.base.mapper.SysRoleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* @author sckj
* @description 针对表【sys_role(角色表)】的数据库操作Service实现
* @createDate 2022-10-19 11:04:48
*/
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole>
    implements SysRoleService{

    @Autowired
    private SysRoleMenuService roleMenuService;
    /**
     * 分页查询所有
     * @param currentPage
     * @param pageSize
     * @return
     */
    @Override
    public Page<SysRole> listByPage(Integer currentPage, Integer pageSize) {
        Page<SysRole> rolePage = new Page<>(currentPage,pageSize);
        baseMapper.selectPage(rolePage, null);
        return rolePage;
    }

    /**
     * 根据姓名分页查询角色信息
     * @param name
     * @param currentPage
     * @param pageSize
     * @return
     */
    @Override
    public Page<SysRole> listSysRoleByNameByPage(String name, Integer currentPage, Integer pageSize) {
        Page<SysRole> rolePage = new Page<>(currentPage,pageSize);
        LambdaQueryWrapper<SysRole> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(SysRole::getName,name);
        baseMapper.selectPage(rolePage, queryWrapper);
        return rolePage;
    }

    /**
     * 按照姓名迷糊查询角色信息
     * @param name
     * @return
     */
    @Override
    public List<SysRole> listSysRoleByName(String name) {
        LambdaQueryWrapper<SysRole> queryChainWrapper = new LambdaQueryWrapper<>();
        queryChainWrapper.like(SysRole::getName,name);
        List<SysRole> sysRoles = baseMapper.selectList(queryChainWrapper);
        return sysRoles;
    }


    /**
     * 根据分页查询所有角色信息
     * @param sysRole
     * @param currentPage
     * @param pageSize
     * @return
     */
    /*@Override
    public Page<SysRole> ListRoleByNameBypage(SysRole sysRole, Integer currentPage, Integer pageSize) {
        Page<SysRole> rolePage = new Page<>(currentPage,pageSize);
        LambdaQueryWrapper<SysRole> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(SysRole::getName,sysRole.getName() == null ? "":sysRole.getName());
        Page<SysRole> roles = baseMapper.selectPage(rolePage, queryWrapper);
        return roles;
    }
*/

    /**
     * 修改角色所具有的菜单权限
     * @param roleId
     * @param menuIds
     * @return
     */
    @Override
    public boolean updateRoleMenus(Integer roleId, List<Integer> menuIds) {
        return roleMenuService.updateMenuByRoleId(roleId, menuIds);
    }

    /**
     * 根据角色ID查询对应的权限
     * @param id
     * @return
     */
    @Override
    public List<SysMenu> listMenuByRoleId(Integer id) {
        List<SysMenu> menus = roleMenuService.listMenuByRoleId(id);
        return menus;
    }


}




